const http = require("http")
const url = require('url');
const sever = http.createServer();
const { readFile,htmlRead,htmlReadWithData,writeFile} = require("./utils/readerFile")
const { insertMysql, list ,deleteItem,selectList,updata,count } = require("./utils/changeData");
const {Message} = require("./class/message")


sever.listen(3000, function () {
    console.log("留言板项目创建")
})

sever.on("request", function (request, response) {
    const {pathname,query} = url.parse(request.url,true)
    // console.log(query)
    if (pathname === "/") {
        // list().then(res => {
        //     htmlReadWithData("./view/index.html", response, res)
        // })
        console.log(query)
        Promise.all([count(query), selectList(query)]).then(res => {
            let total = [];
            const page = Math.ceil(res[0][0].total / (query.pageSize || 8))
            for (let i = 1; i <= page; i++) {
                total.push(i)
            }
            htmlReadWithData("./view/index.html", response, { list: res[1], total })
        })
        
    } else if (pathname.startsWith("/node_modules")) {
        htmlRead("."+request.url,response)
        
    } else if (pathname.startsWith("/publlic")) {
        htmlRead("." + request.url, response)

    } else if (pathname === "/add") {
        htmlRead("./view/add.html", response)
    } else if (pathname === "/post") {
        const message = new Message(query.name, query.message)
        console.log(message);
        insertMysql(message).then(res => {
            response.statusCode = 302;
            response.setHeader("Location", "/")
            response.end()
        })
        
        // readFile("./data.json").then(res => {
        //     const list = JSON.parse(res)
        //     query.time = "2020-11-03"
        //     list.push(query)
        //     return list
        // }).then((list) => {
            
        //     writeFile("./data.json", JSON.stringify(list)).then(res => {
        //         response.statusCode = 302;
        //         response.setHeader("Location", "/")
        //         response.end()
        //     })
        // })
        
        // console.log(list)
        
    }
    else if (pathname === "/update/message") {
        console.log(query);
        updata(query).then(res => {
            console.log("更新成功");
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.end(JSON.stringify({
                success: true,
                msg: "数据更新成功"
            }))
        }).catch(err => {
            console.log(err);
        })
    }
    // 删除
    else if (pathname === "/delete") {
       deleteItem(query.id).then(res => {
            console.log("删除成功");
            // 状态码302代表重定向
            response.statusCode = 302;
            // 重定向到首页
            response.setHeader("Location", "/")
            response.end()
        }).catch(err => {
            console.log(err);
        })
    }
    else {
        htmlRead("./view/404.html",response)
    }
})